﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;

namespace Longbow.Web
{
    /// <summary>
    /// 当前在线用户实体类
    /// </summary>
    public class OnlineUser
    {
        private ConcurrentQueue<KeyValuePair<DateTime, string>> _requestUrls = new ConcurrentQueue<KeyValuePair<DateTime, string>>();

        /// <summary>
        /// 获得/设置 连接ID
        /// </summary>
        public string ConnectionId { get; set; } = "";

        /// <summary>
        /// 获得/设置 当前请求用户名
        /// </summary>
        public string UserName { get; set; } = "";

        /// <summary>
        /// 获得/设置 当前用户显示名称
        /// </summary>
        public string DisplayName { get; set; } = "";

        /// <summary>
        /// 获得/设置 当前连接用户首次访问时刻
        /// </summary>
        public DateTime FirstAccessTime { get; set; }

        /// <summary>
        /// 获得/设置 当前连接用户上次访问时刻
        /// </summary>
        public DateTime LastAccessTime { get; set; }

        /// <summary>
        /// 获得/设置 地理位置
        /// </summary>
        public string Location { get; set; } = "";

        /// <summary>
        /// 获得/设置 请求方法 GET 等
        /// </summary>
        public string Method { get; set; } = "";

        /// <summary>
        /// 获得/设置 当前连接用户 IP 地址
        /// </summary>
        public string Ip { get; set; } = "";

        /// <summary>
        /// 获得/设置 当前连接用户浏览器信息
        /// </summary>
        public string Browser { get; set; } = "";

        /// <summary>
        /// 获得/设置 当前连接用户操作系统信息
        /// </summary>
        public string OS { get; set; } = "";

        /// <summary>
        /// 获得/设置 当前请求地址
        /// </summary>
        public string RequestUrl { get; set; } = "";

        /// <summary>
        /// 获得/设置 客户端请求 User-Agent 信息
        /// </summary>
        public string UserAgent { get; set; } = "";

        /// <summary>
        /// 获得/设置 客户端请求 Referer 信息
        /// </summary>
        public string Referer { get; set; } = "";

        /// <summary>
        /// 获得最近请求地址集合
        /// </summary>
        public IEnumerable<KeyValuePair<DateTime, string>> RequestUrls
        {
            get
            {
                return _requestUrls.ToArray();
            }
        }

        /// <summary>
        /// 添加请求地址到集合中 内部保留 5 个请求地址
        /// </summary>
        /// <param name="url"></param>
        public void AddRequestUrl(string url)
        {
            _requestUrls.Enqueue(new KeyValuePair<DateTime, string>(DateTime.Now, url));
            if (_requestUrls.Count > 5)
            {
                _requestUrls.TryDequeue(out _);
            }
        }
    }
}
